package com.young.config.service.impl;

import com.young.common.util.CollectionUtil;
import com.young.config.model.ConfigKv;
import com.young.config.model.EConfigStateEnum;
import com.young.config.service.IConfigKvService;
import com.young.interfaces.config.model.ConfigDTO;
import com.young.interfaces.config.service.IConfigOutService;
import com.young.interfaces.log.annotation.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 配置服务
 * Created by Admin on 2021/6/20.
 */
@Service
public class ConfigOutServiceImpl implements IConfigOutService {
    /**
     * 配置kv服务
     */
    @Autowired
    private IConfigKvService configKvService;

    /**
     * 查询应用配置
     *
     * @return
     */
    @Log("查询应用配置")
    @Override
    public Map<String, String> getApplicationConfig() {
        Map<String, String> map = new HashMap<String, String>();
        ConfigKv configKv = new ConfigKv();
        configKv.setState(EConfigStateEnum.ENABLE.getCode());
        List<ConfigKv> list = configKvService.getList(configKv);
        if (CollectionUtil.isNotEmpty(list)) {
            for (ConfigKv config : list) {
                map.put(config.getConfigKey(), config.getConfigValue());
            }
        }
        return map;
    }

    /**
     * 查询应用配置
     *
     * @return
     */
    @Log("查询应用配置列表")
    @Override
    public List<ConfigDTO> getApplicationConfigList() {
        ConfigKv configKv = new ConfigKv();
        configKv.setState(EConfigStateEnum.ENABLE.getCode());
        List<ConfigKv> list = configKvService.getList(configKv);
        if (CollectionUtil.isNotEmpty(list)) {
            return list.stream().map(ConfigKv::toConfigDTO).collect(Collectors.toList());
        }
        return null;
    }
}
